package com.fsm.system.serviceImpl;

import com.fsm.common.tools.Result.Result;
import com.fsm.system.entity.table.TRoleMenuTable;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.fsm.system.entity.TRoleMenu;
import com.fsm.system.mapper.TRoleMenuMapper;
import com.fsm.system.service.TRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 *  角色菜单关系服务层实现。
 *
 * @author
 * @since
 */
@Service
public class TRoleMenuServiceImpl extends ServiceImpl<TRoleMenuMapper, TRoleMenu> implements TRoleMenuService {

    /**
     * 公共整合查询条件
     * @param tRoleMenu
     * @return
     */
    private static QueryWrapper getSelectQueryWrapper(TRoleMenu tRoleMenu){
        QueryWrapper queryWrapper = new QueryWrapper();
        return queryWrapper;
    }

    /**
     * 添加角色菜单关系
     *
     * @param tRoleMenu
     * @return
     */
    @Override
    @Transactional
    public Result addTRoleMenu(TRoleMenu tRoleMenu) throws Exception {
        //删除所有的角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(TRoleMenuTable.T_ROLE_MENU.ROLE_ID.eq(tRoleMenu.getRoleId()));
        this.getMapper().deleteByQuery(queryWrapper);
        //循环添加
        List<String> menuList = tRoleMenu.getMenuIds();
        for(String menuId : menuList){
            TRoleMenu tRoleMenuOne = new TRoleMenu();
            tRoleMenuOne.setRoleId(tRoleMenu.getRoleId());
            tRoleMenuOne.setMenuId(menuId);
            int sign = this.getMapper().insert(tRoleMenuOne);
            if(sign == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.fail("添加角色菜单关系失败");
            }
        }
        return Result.ok();
    }

    /**
     * 删除角色菜单关系
     *
     * @param tRoleMenu
     * @return
     */
    @Transactional
    @Override
    public Result deleteTRoleMenu(TRoleMenu tRoleMenu) throws Exception {
        int sign = this.getMapper().deleteById(tRoleMenu.getRoleId());
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("删除角色菜单关系失败");
        }
        return Result.ok();
    }

    /**
     * 更新角色菜单关系
     *
     * @param tRoleMenu
     * @return
     */
    @Transactional
    @Override
    public Result updateTRoleMenu(TRoleMenu tRoleMenu) throws Exception {
        int sign = this.getMapper().update(tRoleMenu);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("更新角色菜单关系失败");
        }
        return Result.ok();
    }

    /**
     * 分页查询角色菜单关系
     *
     * @param tRoleMenu
     * @return
     */
    @Override
    public Result pageTRoleMenu(TRoleMenu tRoleMenu) throws Exception {
        QueryWrapper queryWrapper = getSelectQueryWrapper(tRoleMenu);
        Page<TRoleMenu> tRoleMenuPage = this.getMapper().paginateWithRelations(tRoleMenu.getPage(),tRoleMenu.getLimit(),queryWrapper);
        return Result.ok(tRoleMenuPage.getRecords(),tRoleMenuPage.getTotalRow());
    }

    /**
     * 查询角色菜单关系
     *
     * @param tRoleMenu
     * @return
     */
    @Override
    public Result getTRoleMenuList(TRoleMenu tRoleMenu) throws Exception{
        QueryWrapper queryWrapper = getSelectQueryWrapper(tRoleMenu);
        List<TRoleMenu> tRoleMenuList = this.getMapper().selectListWithRelationsByQuery(queryWrapper);
        return Result.ok(tRoleMenuList,tRoleMenuList.size());
    }

    /**
     * 通过ID查询角色菜单关系
     *
     * @param tRoleMenu
     * @return
     */
    @Override
    public Result getTRoleMenuById(TRoleMenu tRoleMenu) throws Exception{
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(TRoleMenuTable.T_ROLE_MENU.ROLE_ID.eq(tRoleMenu.getRoleId()));
        List<TRoleMenu> tRoleMenuList = this.getMapper().selectListByQuery(queryWrapper);
        return Result.ok(tRoleMenuList);
    }
}
